package com.example.cinema.toolclass;


import android.graphics.Bitmap;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.util.Hashtable;

public class ORCode {
    private int width = 250;
    private int heigh = 250;

    /**
     * 二维码生成
     * @param url 链接地址
     * @return
     */
    public Bitmap CreateORImage(String url)
    {
        Bitmap bitmap = null;
        if(url==null)
        {
            return null;
        }
        Hashtable<EncodeHintType,String> hints = new Hashtable<EncodeHintType,String>();
        hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
        BitMatrix matrix = null;
        try {
            matrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE,width,heigh,hints);
            int[] pixels = new int[width*heigh];
            for (int y=0;y<heigh;y++)
            {
                for (int x=0;x<width;x++)
                {
                    if(matrix.get(x,y))
                    {
                        pixels[y*width+x] = 0xFF000000;
                    }else {
                        pixels[y*width+x] = 0xFFFFFFFF;
                    }
                }
            }
            bitmap = Bitmap.createBitmap(width,heigh,Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels,0,width,0,0,width,heigh);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

}
